const app = getApp();
import md5 from '../../utils/md5.js';
// import trigger from '../../utils/util.js'
Page({
	data: {
		top_num: 0,
		rule_on: '',
		resourceDomain: app.resourceDomain,
		width: '0%',
		help_on: '',
    isShow: false
	},
	form_id_get: function(e) {
		app.request(app.apiDomain + "index.php?c=user&a=addFormId", {
			form_id: e.detail.formId,
			type: form_id
		}, function(res) {
			console.log(res.data.info);
		});
	},
	onLoad: function(options) {
    app.updateFlag(this)
		var that=this;
    that.setData({
      options: options
    })
    console.log(that.data.options)
		var scene = decodeURIComponent(options.scene);
		if (typeof scene !== 'undefined') {
			var scene_obj = app.scene_decode(scene);
			//转换
			if (scene_obj.id) {
				options.goods_id = scene_obj.id;
			};
			if (scene_obj.uid) {
				options.user_id = scene_obj.uid
			};
		};
    // (trigger.trigger)(that, '../mall/Help', options.goods_id, '', '', options.user_id)
		//规则
		app.request(app.apiDomain + "index.php?c=mall&a=notice", {
				type:'assist'
		}, function (res) {
				that.setData({
						gao:res.data.data
				});
		});


		const vm = this;
		//
		setTimeout(function(e = vm) {
			e.setData({
				help_on: 'on'
			})
		}, 1000);
		// console.log('options'+options);
		var num = 0;

		var off = true;
		setInterval(function() {
			if (off) {
				num++
				vm.setData({
					top_num: 6 * num + 'vw'
				})
				if (num == 5) {
					off = false;
				}
			} else {
				num--
				vm.setData({
					top_num: 6 * num + 'vw'
				})
				if (num == 0) {
					off = true;
				}
			}
		}, 4000);

		var user_id = wx.getStorageSync('user_id');
		var nickname = wx.getStorageSync('nickname');
		var openid = wx.getStorageSync('openid');
		var getinfobutton = true;

	
		this.setData({
			getinfobutton: getinfobutton
		});

		var that = this;
    that.kanjia()
		// app.request(app.apiDomain + 'index.php?c=mall&a=assistdetail', {
		// 		goods_id: options.goods_id,
		// 		user_id: options.user_id,
		// 	},
		// 	function(res) {
		// 		if (res.data.status == '1') {
		// 			var goods = res.data.data.goods;
		// 			that.setData({
		// 				goods: goods,
		// 				assistlist: res.data.data.assistlist,
		// 				user: res.data.data.user,
		// 				self_user_id: res.data.data.self_user_id,
		// 			});
		// 			that.setData({
		// 				width: (100 / goods.f_need_assist_num) * goods.has_assist_num + '%'
		// 			});

		// 		} else {
		// 			wx.showToast({
		// 				title: res.data.info,
		// 				icon: 'none'
		// 			});
		// 			setTimeout(function() {
		// 				wx.navigateTo({
		// 					url: '../mall/index'
		// 				})
		// 			}, 2000);

		// 		}
		// 	});

	},
  authSuccess(e) {
    this.setData({
      isShow: false
    })
    this.kanjia()
  },
  authFail(e) {
    this.setData({
      isShow: false
    })
  },
  click() {
    this.setData({
      isShow: false
    })
  },
  onReady() {
    app.SystemInfo(this)
  },
  return_back() {
    let pages = getCurrentPages()
    if (pages.length == 1) {
      wx.redirectTo({
        url: '../index/index',
      })
    } else {
      wx.navigateBack({
        delta: 1
      })
    }
  },
  return_act() {
    wx.redirectTo({
      url: '../mall/index',
    })
  },
  linkTo (e) {
    let url = e.target.dataset.url
    wx.navigateTo({
      url: url,
    })
  },
  onPullDownRefresh: function () {
    console.log('下拉刷新')
    var that = this
    console.log(that.data.options)
    wx.showLoading({
      title: '加载中',
    })
    app.request(app.apiDomain + 'index.php?c=bargain&a=detail', {
      goods_id: that.data.options.goods_id,
      user_id: that.data.options.user_id,
    }, res => {
      console.log(res)
      wx.hideLoading()
      if (res.data.status == '1') {
        var goods = res.data.data.goods
        if (!res.data.data.goods.self_assist) {
          that.setData({
            close_mask: false,
            goods: res.data.data.goods,
            assist_price: parseFloat(res.data.data.assist_price).toFixed(2),
            assistlist: res.data.data.assistlist,
            user: res.data.data.user,
            total: res.data.data.total,
            self_user_id: res.data.data.self_user_id,
            width: ((goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100).toFixed(1)
          })
        } else {
          if (res.data.data.goods.has_assist != '1') {
            that.setData({
              close_mask: true,
              goods: res.data.data.goods,
              assist_price: parseFloat(res.data.data.assist_price).toFixed(2),
              assistlist: res.data.data.assistlist,
              user: res.data.data.user,
              total: res.data.data.total,
              self_user_id: res.data.data.self_user_id,
              width: ((goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100).toFixed(1)
            })
          } else {
            that.setData({
              close_mask: false,
              goods: res.data.data.goods,
              assist_price: parseFloat(res.data.data.assist_price).toFixed(2),
              assistlist: res.data.data.assistlist,
              user: res.data.data.user,
              total: res.data.data.total,
              self_user_id: res.data.data.self_user_id,
              width: ((goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100).toFixed(1)
            })
          }
        }
        wx.stopPullDownRefresh()
      } else {
        wx.showToast({
          title: res.data.info,
          icon: "none"
        })
        wx.stopPullDownRefresh()
      }
    })
  },
	gao_click:function(){
		this.setData({
			gao_on:'on'
		})
	},
  kanjia: function () {
    var that = this
    console.log(that.data.options)
    wx.showLoading({
      title: '加载中',
    })
    app.request(app.apiDomain + 'index.php?c=bargain&a=detail', {
      goods_id: that.data.options.goods_id,
      user_id: that.data.options.user_id,
    }, res => {
      console.log(res)
      if (res.data.status == '1') {
        var goods = res.data.data.goods
        if (!res.data.data.goods.self_assist) {
        that.setData({
          close_mask: false,
          goods: res.data.data.goods,
          assist_price: parseFloat(res.data.data.assist_price).toFixed(2),
          assistlist: res.data.data.assistlist,
          user: res.data.data.user,
          total: res.data.data.total,
          self_user_id: res.data.data.self_user_id,
          width: ((goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100).toFixed(1)
        })
        } else {
        if (res.data.data.goods.has_assist != '1') {
          that.setData({
            close_mask: true,
            goods: res.data.data.goods,
            assist_price: parseFloat(res.data.data.assist_price).toFixed(2),
            assistlist: res.data.data.assistlist,
            user: res.data.data.user,
            total: res.data.data.total,
            self_user_id: res.data.data.self_user_id,
            width: ((goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100).toFixed(1)
          })
        } else {
          that.setData({
            close_mask: false,
            goods: res.data.data.goods,
            assist_price: parseFloat(res.data.data.assist_price).toFixed(2),
            assistlist: res.data.data.assistlist,
            user: res.data.data.user,
            total: res.data.data.total,
            self_user_id: res.data.data.self_user_id,
            width: ((goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100).toFixed(1)
          })
        }
        }
      } else {
        wx.showToast({
          title: res.data.info,
          icon: "none"
        })
      }
      wx.hideLoading()
    })
  },
  close_mask: function () {
    var that = this
    that.setData({
      close_mask: false
    })
  },
	gao_off:function(){
		this.setData({
			gao_on:''
		})
	},
	// getUserInfo: function() {
	// 	var that = this;
	// 	wx.showLoading({
	// 		title: '信息授权中',
	// 	});
	// 	wx.getUserInfo({
	// 		lang: "zh_CN",
  //     fail: function () {
  //       wx.hideLoading()
  //     },
	// 		success: res => {
	// 			var userInfo = res.userInfo;
	// 			var openid = wx.getStorageSync("openid");
	// 			app.request(app.apiDomain + 'index.php?c=user&a=profile', {
	// 				nickname: userInfo.nickName,
	// 				gender: userInfo.gender,
	// 				icon: userInfo.avatarUrl,
	// 				zone_name: userInfo.province + ' ' + userInfo.city,
	// 				invite_user: wx.getStorageSync('invite_user_user_id'),
	// 			}, function(res) {
	// 				setTimeout(function() {
	// 					wx.hideLoading()
	// 				}, 2000);
	// 				if (res.data.status == 1) {
	// 					wx.showToast({
	// 						title: '授权完成,你可以继续操作了',
	// 						icon: "none"
	// 					});
	// 					that.setData({
	// 						getinfobutton: false,
	// 					});
	// 					app.setWxUserInfo(userInfo);
	// 					console.log(openid);

	// 				} else {
	// 					wx.navigateTo({
	// 						url: '../index/index'
	// 					})
	// 				}
	// 			});
	// 		},
	// 	})
	// },
	assist: function(e) {
		//进行助力
		var that = this;
    if (!app.isAuth) {
      that.setData({
        isShow: true
      })
    } else {
      var time2 = Date.parse(new Date()).toString() / 1000;
      var openid2 = wx.getStorageSync('openid')
      app.request(app.apiDomain + 'index.php?c=bargain&a=assist', {
        id: e.target.dataset.id,
        time: time2,
        f_channel: 'xiaochengxu',
        sign: md5(encodeURI('f_channel=xiaochengxu&id=' + e.target.dataset.id + '&openid=' + openid2 + '&time=' + time2 +
          '&key=6723d2909ec9fbf969c000e32ff2f514'))
      }, function (res) {
        if (res.data.status == '1') {
          var goods = res.data.data.goods;
          // wx.showToast({
          // 	title: '砍价成功，谢谢你的支持',
          // });
          that.setData({
            close_mask: true
          })
          that.setData({
            goods: goods,
            assistlist: res.data.data.assistlist,
            assist_price: parseFloat(res.data.data.bargain.bargain_price).toFixed(2),
            user: res.data.data.user,
            total: res.data.data.total,
            self_user_id: res.data.data.self_user_id,
            width: (goods.f_price - goods.current_price) / (goods.f_price - goods.f_min_price) * 100
          });
        } else {
          wx.showToast({
            title: res.data.info,
            icon: "none"
          })
        }
      });
      const innerAudioContext = wx.createInnerAudioContext('zan')
      innerAudioContext.autoplay = true
      innerAudioContext.src = 'http://resource.160nurse.com/canvas/healthy/img/zou_lu/zan.mp3'
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      });
    }

	},
	rule_click: function() {
		this.setData({
			rule_on: 'on'
		});
	},
	rule_off: function() {
		this.setData({
			rule_on: ' '
		});
	},
	onShareAppMessage: function() {
		var path = "/pages/mall/Help?user_id=" + this.data.goods.user_id + '&goods_id=' + this.data.goods.id;
		console.log(path);
		return {
			path: path,
      title: wx.getStorageSync('nickname') + '正在参加砍价活动，就差一点了，快点来帮TA',
		}
	},
  // getUserInfo: function () {
  //   var that = this
  //   if (app.getUserInfo(that, this.options)) {
  //     that.setData({
  //       user_auth: false
  //     })
  //   }
  // },
  onShow: function () {
    
  }
})
